#include "medicalrecords.h"
#include "messagetype.h"
#include <QFont>
#include <QPalette>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTextEdit>
#include <QLineEdit>

MedicalRecords::MedicalRecords(QTcpSocket *socket, QWidget *parent)
    : QWidget(parent), tcpSocket(socket) {

    // 设置窗口标题和大小
    setWindowTitle("查看病例");
    setFixedSize(600, 700);  // 增大窗口尺寸，给控件提供更多的空间

    QVBoxLayout *layout = new QVBoxLayout(this);

    // 设置窗口背景颜色
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, QColor(240, 248, 255)); // 淡蓝色背景
    this->setPalette(palette);
    this->setAutoFillBackground(true);

    // 添加标题标签
    QLabel *label = new QLabel("查看病例", this);
    label->setFont(QFont("微软雅黑", 20, QFont::Bold));
    label->setAlignment(Qt::AlignCenter);
    layout->addWidget(label);

    // 添加患者ID输入
    QLabel *patientIdLabel = new QLabel("患者ID:", this);
    patientIdLabel->setFont(QFont("微软雅黑", 16));
    layout->addWidget(patientIdLabel);
    patientIdEdit = new QLineEdit(this);
    patientIdEdit->setPlaceholderText("请输入患者ID");
    patientIdEdit->setFont(QFont("微软雅黑", 14));
    patientIdEdit->setFixedHeight(40);
    layout->addWidget(patientIdEdit);

    // 添加患者姓名输入
    QLabel *patientNameLabel = new QLabel("患者姓名:", this);
    patientNameLabel->setFont(QFont("微软雅黑", 16));
    layout->addWidget(patientNameLabel);
    patientNameEdit = new QLineEdit(this);
    patientNameEdit->setPlaceholderText("请输入患者姓名");
    patientNameEdit->setFont(QFont("微软雅黑", 14));
    patientNameEdit->setFixedHeight(40);
    layout->addWidget(patientNameEdit);

    // 添加查询按钮
    QPushButton *queryButton = new QPushButton("查询病例", this);
    queryButton->setFont(QFont("微软雅黑", 16, QFont::Bold));
    queryButton->setStyleSheet("QPushButton {"
                               "background-color: #4CAF50;"
                               "color: white;"
                               "border-radius: 8px;"
                               "padding: 12px;"
                               "} "
                               "QPushButton:hover {"
                               "background-color: #45A049;"
                               "}");
    queryButton->setFixedHeight(50);
    layout->addWidget(queryButton);

    // 添加病例显示框
    recordDisplay = new QTextEdit(this);
    recordDisplay->setReadOnly(true);
    recordDisplay->setFont(QFont("微软雅黑", 14));
    recordDisplay->setStyleSheet("padding: 10px; border: 1px solid #ccc; border-radius: 5px; background-color: #ffffff;");
    layout->addWidget(recordDisplay);

    // 添加返回按钮
    QPushButton *backButton = new QPushButton("返回", this);
    backButton->setFont(QFont("微软雅黑", 16, QFont::Bold));
    backButton->setStyleSheet("QPushButton {"
                              "background-color: #f44336;"
                              "color: white;"
                              "border-radius: 8px;"
                              "padding: 12px;"
                              "} "
                              "QPushButton:hover {"
                              "background-color: #e53935;"
                              "}");
    backButton->setFixedHeight(50);

    // 添加按钮布局使其居中
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch();
    buttonLayout->addWidget(backButton);
    buttonLayout->addStretch();

    layout->addLayout(buttonLayout);

    // 设置主布局的边距和间距
    layout->setSpacing(20); // 增加控件之间的间距
    layout->setContentsMargins(30, 30, 30, 30); // 设置边距

    // 连接信号与槽
    connect(queryButton, &QPushButton::clicked, this, &MedicalRecords::onQueryButtonClicked);
    connect(backButton, &QPushButton::clicked, this, &MedicalRecords::onBackButtonClicked);
}

void MedicalRecords::onQueryButtonClicked() {
    // 获取输入的患者ID和姓名
    QString patientId = patientIdEdit->text();
    QString patientName = patientNameEdit->text();

    // 校验输入是否为空
    if (patientId.isEmpty() || patientName.isEmpty()) {
        recordDisplay->setText("请输入完整的患者信息");
        return;
    }

    // 构造查询请求的数据包
    QMap<QString, QString> queryData;
    queryData.insert("type", MEDICIALRECDATABASE);
    queryData.insert("databaseoperator", QString::number(SELECT));
    queryData.insert("patientid", patientId);
    queryData.insert("patientname", patientName);
    QByteArray data = mapToByteArrayCustom(queryData);

    // 连接服务器并发送请求
    if (tcpSocket->state() == QAbstractSocket::UnconnectedState) {
        tcpSocket->connectToHost("127.0.0.1", 12345);
    }
    if (tcpSocket->waitForConnected(3000)) {
        tcpSocket->write(data);
        tcpSocket->flush();

        if (tcpSocket->waitForReadyRead(3000)) {
            QByteArray responseData = tcpSocket->readAll();
            // 解析响应的数据
            QList<QMap<QString, QString>> medicalRecords = convertByteArrayToQMapArrayCustom(responseData);

            // 展示病例信息
            if (!medicalRecords.isEmpty()) {
                QString displayText;
                for (const QMap<QString, QString>& record : medicalRecords) {
                    displayText += "药物: " + record.value("medic") + "\n";
                    displayText += "医生: " + record.value("doctorname") + "\n";
                    displayText += "-----------------------\n";
                }
                recordDisplay->setText(displayText);
            } else {
                recordDisplay->setText("未找到相关病例记录");
            }
        } else {
            recordDisplay->setText("查询超时，未能接收到服务器响应");
        }
    } else {
        recordDisplay->setText("无法连接到服务器，请检查网络连接");
    }
}

void MedicalRecords::onBackButtonClicked() {
    this->close();  // 关闭窗口，返回上一级
}
